package com.bootssm.controller;

import com.bootssm.dao.PlayerMapper;
import com.bootssm.enity.PageView;
import com.bootssm.enity.Player;
import com.bootssm.enity.PlayerQueryVO;
import com.bootssm.enity.Result;
import com.bootssm.service.PlayerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/player")
public class PlayerController {

    @Autowired
    PlayerService playerService;

    @PostMapping("/findAll")
    public PageView<Player> findAll(@RequestBody PlayerQueryVO playerQueryVO) {
        Integer page=playerQueryVO.getPage();
        Integer start = (page-1)*playerQueryVO.getSize();
        playerQueryVO.setStart(start);
        List<Player> list = playerService.findAll(playerQueryVO);

        Integer count = playerService.getCount(playerQueryVO);
        PageView<Player> pageView = new PageView<>();
        pageView.setDataList(list);
        pageView.setCount(count);
        return pageView;
    }

    @GetMapping("/deleteById/{id}")
    public Result deletePlayer(@PathVariable("id") String playerid) {
        try {
            playerService.deletePlayer(playerid);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("500", "delete wrong!");
        }
        return new Result("200", "delete!");
    }

    @PostMapping("/addPlayer")
    public Result addPlayer(@RequestBody Player player) {
        try {
            playerService.addPlayer(player);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("500", "add wrong!");
        }
        return new Result("200", "add!");
    }

    @PostMapping("/updatePlayer")
    public Result updatePlayer(@RequestBody Player player) {
        try {
            playerService.updatePlayer(player);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("500", "update wrong!");
        }
        return new Result("200", "update!");
    }

    @GetMapping("/findById/{id}")
    public Player findById(@PathVariable("id") String playerId) {
        Player player= playerService.findById(playerId);
        return player;

    }
}
